<%@ page import="java.util.List" %>
<%@ page import="com.zwy.Shopping.model.CartItem" %>
<%@ page import="java.math.BigDecimal" %>
<%@ page import="com.zwy.Shopping.model.User" %>
<%@ page import="com.zwy.Shopping.dao.OrderDao" %>
<%@ page import="com.zwy.Shopping.dao.impl.OrderDaoImpl" %>
<%@ page import="com.zwy.Shopping.model.Order" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/bootstrap.min.css">
<script src="js/bootstrap.bundle.js"></script>
<script>
    function upTotal(total) {
        var xhr = new XMLHttpRequest();
        xhr.open("post", "buy_all_in_cartItem", true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.onreadystatechange = function () {//回调函数,readystate改变调用
            if (xhr.readyState === 4 && xhr.status === 200) {
                //刷新当前页面
                location.reload();
                // 请求已完成且成功
                console.log(xhr.responseText); // 打印服务器的响应内容
                // 在这里您可以处理响应，比如更新页面上的某些元素
            } else if (xhr.readyState === 4 && xhr.status !== 200) {
                // 请求已完成但出现了错误
                console.error("请求失败: " + xhr.statusText);
            }
        };
        xhr.send("total=" + total);
    }

    function delCartItem(index) {
        var xhr = new XMLHttpRequest();
        xhr.open("post", "del_cartItem_by_index", true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.onreadystatechange = function () {//回调函数,readystate改变调用
            if (xhr.readyState === 4 && xhr.status === 200) {
                //请求成功后,刷新当前页面,
                location.reload();
                // 请求已完成且成功
                console.log(xhr.responseText); // 打印服务器的响应内容
                // 在这里您可以处理响应，比如更新页面上的某些元素
            } else if (xhr.readyState === 4 && xhr.status !== 200) {
                // 请求已完成但出现了错误
                console.error("请求失败: " + xhr.statusText);
            }
        };
        // 注意这个index是要获取到用户的cartItemList后在使用
        xhr.send("index=" + index);

    }
</script>
<head>
    <meta charset="UTF-8">
    <title>购物车</title>
    <jsp:include page="includes/nav.jsp"></jsp:include>
</head>
<body>
<% User user = (User) request.getSession().getAttribute("user");
    //OrderItemDao orderItemDao = new OrderItemDaoImpl();
    //判断是否登陆没有登录不给打开
    if (user == null) {
        request.setAttribute("message", "请登录后查看");
        request.setAttribute("page", "login.jsp");
        request.getRequestDispatcher("alert.jsp").forward(request, response);
    }


%>
<div class="container-fluid mt-3">

    <% List<CartItem> cartItemList = (List<CartItem>) application.getAttribute("cartItemList");
        BigDecimal total = BigDecimal.valueOf(0);
        //构建一个list的指数,用于删除功能
        int index = -1;
        if (cartItemList != null) {
            for (CartItem cartItem : cartItemList) {
                //从cartItemList找出对应user的购物车信息的total
                if (cartItem.getUserId() == user.getId())
                    total = total.add(cartItem.getTotal());
            }
        }
        request.setAttribute("total", total);%>

    <h2 class="d-block" style="margin-top: 40px;">总金额:<%=request.getAttribute("total")%>元
        <button
                class="btn btn-primary" type="button" onclick="upTotal(<%=request.getAttribute("total")%>)">支付
        </button>
    </h2>
    <!--表格-->

    <table class="table table-striped table-hover table-success">
        <thead>
        <tr>
            <th>商品名和图片</th>
            <th>类别</th>
            <th>价格</th>
            <th>数量</th>
            <th>取消</th>
        </tr>
        </thead>
        <tbody>
        <%
            if (cartItemList == null) {
                System.out.println("购物车为空");
            }%>
        <%
            if (cartItemList != null) {
                for (CartItem cartItem : cartItemList) {
                    if (cartItem.getUserId() == user.getId()) {
                        index++;

        %>
        <tr>
            <td><%=cartItem.getProduct().getName()%> <img class="img-thumbnail"
                                                          src="img/<%=cartItem.getProduct().getImage()%>"
                                                          style="width: 100px;"></td>
            <td class="align-content-center"><%=cartItem.getProduct().getCategory().getName()%>
            </td>
            <td class="align-content-center"><%=cartItem.getProduct().getPrice()%> 元</td>
            <td class="align-content-center"><%=cartItem.getQuantity()%>
            </td>
            <td class="align-content-center">
                <button class="btn btn-danger" type="button" onclick="delCartItem(<%=index%>)"> 删除</button>
            </td>
        </tr>
        <%
                    }
                }
            }
        %>


        </tbody>
    </table>
</div>

</body>
</html>